package com.example.itmanager.activity;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;

/**
 * @author 23737
 * @version 1.0
 * @time 2021.8.8
 */
public abstract class BaseActivity extends AppCompatActivity {
    public Context mcontext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext = this;
        setContentView(initLayout());
        initView();
        initData();
    }

    protected abstract int initLayout();

    protected abstract void initView();

    protected abstract void initData();

    //对跳转进行封装
    public void navigateTo(Class cls){
        Intent intent = new Intent(mcontext, cls);
        startActivity(intent);
    }

    //异步进行跳转
    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(mcontext, cls);
        in.setFlags(flags);
        startActivity(in);
    }

    public void showToast(String msg) {
        Toast.makeText(mcontext, msg, Toast.LENGTH_SHORT).show();
    }

    public void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(mcontext, msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    //存入token
    protected void insertVal(String key, String val) {
        SharedPreferences sp = getSharedPreferences("sp_it", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.commit();
    }

    protected String findByKey(String key) {
        SharedPreferences sp = getSharedPreferences("sp_it", MODE_PRIVATE);
        return sp.getString(key, "");
    }
}
